﻿Public Partial Class Index
    Inherits System.Web.UI.Page

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Anthem.Manager.Register(Me)
        '  Anthem.Manager.AddScriptForClientSideEval("jQuery('iframe').iframeAutoHeight();")

        If Session("usuario") Is Nothing Then
            'RemoveControls()
            'CargaControl("Wctl_Login.ascx", False)
            'TreeView1.Visible = False
            Me.Label1.Visible = False

            'Me.Panel1.Width = Unit.Percentage(0)
            'td_menu.Width = "0px"
            'Anthem.Manager.AddScriptForClientSideEval("ocument.getElementById('div_menu').visible=")
            Exit Sub
        End If

        'If Not ViewState("usercontrol") Is Nothing Then
        '    If ViewState("usercontrol") <> "" Then
        '        CargaControl(ViewState("usercontrol"), False)
        '    End If
        'End If
     
        'TreeView1.Visible = True
        Label1.Visible = True



        If Not Page.IsPostBack Then
            ViewState("usuario") = Session("usuario")
            ViewState("perfil") = "1"
            CrearMenu2()
            'CrearMenu()
        End If
    End Sub

    'Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
    '    If Session("usuario") Is Nothing Then
    '        RemoveControls()
    '        CargaControl("Wctl_Login.ascx", False)
    '        Exit Sub
    '    End If
    '    CargaControl(e.Item.Value.Replace(".ascx", "") & ".ascx", True)
    'End Sub
    'Sub CargaControl(ByVal ControlTxt As String, ByVal remove As Boolean)

    '    Try
    '        If remove Then
    '            RemoveControls()
    '            'Try
    '            '    Dim LastFeaturedProductUserControl As Control
    '            '    LastFeaturedProductUserControl = LoadControl(ViewState("usercontrol"))
    '            '    Panel2.Controls.Remove(LastFeaturedProductUserControl)

    '            '    Dim i As Integer
    '            '    For i = 0 To Panel2.Controls.Count - 1
    '            '        Panel2.Controls.Remove(Panel2.Controls(i))
    '            '        Panel2.UpdateAfterCallBack = True
    '            '    Next

    '            'Catch ex As Exception

    '            'End Try
    '        End If

    '        Dim FeaturedProductUserControl As Control
    '        FeaturedProductUserControl = LoadControl(ControlTxt)

    '        Controls.Add(FeaturedProductUserControl)
    '        'PlaceHolder1.Controls.Add(FeaturedProductUserControl)
    '        Panel1.Controls.Add(FeaturedProductUserControl)

    '        ViewState("usercontrol") = ControlTxt

    '        AddControls(ControlTxt)

    '        ' PlaceHolder1.UpdateAfterCallBack = True
    '    Catch ex As Exception

    '        ViewState("usercontrol") = ""
    '    End Try

    'End Sub
    'Sub AddControls(ByVal control As String)

    '    If ViewState("controles") Is Nothing Then
    '        ViewState("controles") = control
    '    Else
    '        ViewState("controles") = ViewState("controles") & ";" & control
    '    End If


    'End Sub
    'Sub RemoveControls()
    '    Try
    '        Dim i As Integer
    '        Dim j As Integer = Panel1.Controls.Count - 1 ' PlaceHolder1.Controls.Count - 1

    '        For i = 0 To PlaceHolder1.Controls.Count - 1
    '            'PlaceHolder1.Controls.Remove(PlaceHolder1.Controls(j))
    '            Panel1.Controls.Remove(Panel1.Controls(j))
    '            Panel1.UpdateAfterCallBack = True
    '            'PlaceHolder1.UpdateAfterCallBack = True
    '            j = j - 1
    '        Next
    '        If Not ViewState("controles") Is Nothing Then



    '            For i = 0 To ViewState("controles").ToString.Split(";").Length - 1

    '                Try
    '                    Dim LastFeaturedProductUserControl As Control
    '                    LastFeaturedProductUserControl = LoadControl(ViewState("controles").ToString.Split(";")(i))
    '                    'PlaceHolder1.Controls.Remove(LastFeaturedProductUserControl)
    '                    Panel1.Controls.Remove(LastFeaturedProductUserControl)
    '                    Panel1.UpdateAfterCallBack = True
    '                    'PlaceHolder1.UpdateAfterCallBack = True

    '                Catch ex As Exception

    '                End Try


    '            Next
    '        End If
    '        ViewState("controles") = Nothing
    '    Catch ex As Exception

    '    End Try
    'End Sub

    ''Menu1

    'Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.SelectedNodeChanged
    '    If Session("usuario") Is Nothing Then
    '        RemoveControls()
    '        CargaControl("Wctl_Login.ascx", False)
    '        Exit Sub
    '    End If

    '    CargaControl(TreeView1.SelectedNode.Target.Replace(".ascx", "") & ".ascx", True)
    '    Dim dd = Panel1.Controls.Count
    '    'DirectCast(Panel1.Controls(1).Controls(5), Anthem.Panel).CssClass = "PanelIngresoDatos  rounded"
    '    'DirectCast(Panel1.Controls(1).Controls(5), Anthem.Panel).UpdateAfterCallBack = True

    '    Anthem.Manager.AddScriptForClientSideEval("$('.rounded').corners();")
    'End Sub

    'Sub CrearMenu()
    '    Dim exec As New DotNetDal.SQLPersistence(False, "cnn")
    '    Dim rp As New DotNetResponse.SQLPersistence
    '    'Dim csql As String = " SELECT * from sys_menu,SYS_PERFIL_MENU where visible='S' and nid_padre = '0' and pk_id_menu = fk_id_menu and fk_id_perfil = '" & ViewState("perfil") & "' and fk_id_sistema = " & ViewState("sistema")
    '    rp.StoredProcedure = "SP_SYS_GET_MENU_PADRE"
    '    rp.AddParametro(ViewState("perfil"))
    '    rp.AddParametro(ViewState("usuario"))
    '    exec.ExecuteDataTable(rp)


    '    If rp.TieneDatos Then
    '        Dim i As Integer
    '        For i = 0 To rp.DtTable.Rows.Count - 1
    '            Dim nod As New TreeNode ' MenuItem
    '            'nod.Target = "mainFrame"
    '            nod.Text = rp.DtTable.Rows(i).Item("snombre").ToString
    '            nod.ToolTip = rp.DtTable.Rows(i).Item(0).ToString
    '            nod.Value = rp.DtTable.Rows(i).Item(0).ToString

    '            If rp.DtTable.Rows(i).Item("bfolder").ToString.ToUpper <> "S" Then
    '                'nod.Target = "mainFrame"
    '                'If Len(rp.DtTable.Rows(i).Item("surl").ToString.Trim) > 0 Then
    '                '    nod.NavigateUrl = rp.DtTable.Rows(i).Item("surl") & "?ID=" & Request("ID")
    '                'End If
    '                'nod.Expanded = True
    '                'nod.ImageUrl = "./Imagenes/item.gif"
    '                'nod.ExpandAll()
    '            Else
    '                ' nod.Expanded = True
    '                'nod.ExpandAll()
    '                'nod.ImageUrl = "./Imagenes/Folder.gif"
    '                subNodos(nod)
    '            End If


    '            'Menu1.Items.Add(nod)
    '            'TreeView1.Nodes.Add(nod)
    '            If Len(rp.DtTable.Rows(i).Item("surl").ToString.Trim) > 0 Then
    '                nod.Target = rp.DtTable.Rows(i).Item("surl") '& "?ID=" & HttpUtility.UrlEncode(Request("P"))
    '            End If

    '            TreeView1.Nodes(0).ChildNodes.Add(nod)


    '        Next
    '    End If
    '    rp.Dispose()
    '    exec.Dispose()

    'End Sub

    'Sub subNodos(ByVal Nodo As TreeNode)
    '    Try

    '        Dim exec As New DotNetDal.SQLPersistence(False, "cnn")
    '        Dim rp As New DotNetResponse.SQLPersistence
    '        rp.StoredProcedure = "SP_SYS_GET_MENU_HIJO"
    '        rp.AddParametro(Nodo.Value)
    '        rp.AddParametro(ViewState("perfil"))
    '        rp.AddParametro(ViewState("usuario"))
    '        exec.ExecuteDataTable(rp)

    '        If rp.TieneDatos Then
    '            Dim i As Integer
    '            For i = 0 To rp.DtTable.Rows.Count - 1
    '                Dim nod As New TreeNode '  MenuItem
    '                nod.Text = rp.DtTable.Rows(i).Item("snombre").ToString
    '                nod.Value = rp.DtTable.Rows(i).Item(0).ToString
    '                'nod.Target = "mainFrame"
    '                If rp.DtTable.Rows(i).Item("bfolder").ToString.ToUpper <> "S" Then
    '                    'nod.Target = "mainFrame"
    '                    'nod.ImageUrl = "./imagenes/item.gif"
    '                Else
    '                    'nod.Expanded = True
    '                    subNodos(nod)
    '                    nod.ImageUrl = "./Imagenes/Folder.gif"
    '                End If

    '                If Len(rp.DtTable.Rows(i).Item("surl").ToString.Trim) > 0 Then
    '                    nod.Target = rp.DtTable.Rows(i).Item("surl") '& "?ID=" & HttpUtility.UrlEncode(Request("P"))
    '                End If
    '                ' nod.Expanded = True
    '                Nodo.ChildNodes.Add(nod)  'ChildItems.Add(nod)                      
    '            Next

    '        End If
    '        exec.Dispose()
    '        rp.Dispose()

    '    Catch ex As Exception

    '    End Try
    'End Sub




    Sub CrearMenu2()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn")
        Dim rp As New DotNetResponse.SQLPersistence
        'Dim csql As String = " SELECT * from sys_menu,SYS_PERFIL_MENU where visible='S' and nid_padre = '0' and pk_id_menu = fk_id_menu and fk_id_perfil = '" & ViewState("perfil") & "' and fk_id_sistema = " & ViewState("sistema")
        rp.StoredProcedure = "SP_SYS_GET_MENU_PADRE"
        rp.AddParametro(ViewState("perfil"))
        rp.AddParametro(ViewState("usuario"))
        exec.ExecuteDataTable(rp)


        If rp.TieneDatos Then
            Dim i As Integer
            For i = 0 To rp.DtTable.Rows.Count - 1
                Dim nod As New MenuItem ' MenuItem
                'nod.Target = "mainFrame"
                nod.Text = rp.DtTable.Rows(i).Item("snombre").ToString
                nod.ToolTip = rp.DtTable.Rows(i).Item(0).ToString
                nod.Value = rp.DtTable.Rows(i).Item(0).ToString

                If rp.DtTable.Rows(i).Item("bfolder").ToString.ToUpper <> "S" Then
                    'nod.Target = "mainFrame"
                    'If Len(rp.DtTable.Rows(i).Item("surl").ToString.Trim) > 0 Then
                    nod.NavigateUrl = rp.DtTable.Rows(i).Item("surl").ToString() '& "?ID=" & Request("ID")
                    nod.Value = rp.DtTable.Rows(i).Item("surl").ToString()
                    'End If
                    'nod.Expanded = True
                    'nod.ImageUrl = "./Imagenes/item.gif"
                    'nod.ExpandAll()
                Else
                    ' nod.Expanded = True
                    'nod.ExpandAll()
                    'nod.ImageUrl = "./Imagenes/Folder.gif"
                    subNodos2(nod)
                End If



                'TreeView1.Nodes.Add(nod)
                If Len(rp.DtTable.Rows(i).Item("surl").ToString.Trim) > 0 Then
                    nod.Target = "myframe" '& "?ID=" & HttpUtility.UrlEncode(Request("P"))
                End If

                Menu1.Items.Add(nod)


            Next
        End If
        rp.Dispose()
        exec.Dispose()

    End Sub

    Sub subNodos2(ByVal Nodo As MenuItem)
        Try

            Dim exec As New DotNetDal.SQLPersistence(False, "cnn")
            Dim rp As New DotNetResponse.SQLPersistence
            rp.StoredProcedure = "SP_SYS_GET_MENU_HIJO"
            rp.AddParametro(Nodo.Value)
            rp.AddParametro(ViewState("perfil"))
            rp.AddParametro(ViewState("usuario"))
            exec.ExecuteDataTable(rp)

            If rp.TieneDatos Then
                Dim i As Integer
                For i = 0 To rp.DtTable.Rows.Count - 1
                    Dim nod As New MenuItem
                    nod.Text = rp.DtTable.Rows(i).Item("snombre").ToString
                    nod.Value = rp.DtTable.Rows(i).Item(0).ToString
                    'nod.Target = "mainFrame"
                    If rp.DtTable.Rows(i).Item("bfolder").ToString.ToUpper <> "S" Then
                        ' nod.Target = "myframe"
                        nod.NavigateUrl = rp.DtTable.Rows(i).Item("surl").ToString()  '& "?ID=" 
                        'nod.ImageUrl = "./imagenes/item.gif"
                        nod.Value = rp.DtTable.Rows(i).Item("surl").ToString()
                    Else
                        'nod.Expanded = True
                        subNodos2(nod)
                        nod.ImageUrl = "./Imagenes/Folder.gif"
                    End If

                    If Len(rp.DtTable.Rows(i).Item("surl").ToString.Trim) > 0 Then
                        nod.Target = "myframe" 'rp.DtTable.Rows(i).Item("surl") '& "?ID=" & HttpUtility.UrlEncode(Request("P"))
                    End If
                    ' nod.Expanded = True
                    Nodo.ChildItems.Add(nod)  'ChildItems.Add(nod)                      
                Next

            End If
            exec.Dispose()
            rp.Dispose()

        Catch ex As Exception

        End Try
    End Sub


    Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
        'Anthem.Manager.Register(Me)
        'Dim Usuario As String = Session("usuario")


        'If Session("usuario") Is Nothing Then
        '    Session.RemoveAll()

        '    Session("usuario") = Usuario

        '    If Anthem.Manager.IsCallBack Then
        '        Anthem.Manager.AddScriptForClientSideEval("try {self.parent.location ='../Inicio.aspx';}catch(ex){}")
        '    Else
        '        Page.ClientScript.RegisterStartupScript(Me.GetType, "inicio", "<script> try {self.parent.location ='../Inicio.aspx';}catch(ex){} </script>")
        '    End If
        'Else
        '    ' document.all("myframe").src= DirectCast(sender,System.Web.UI.WebControls.Menu).SelectedValue  parent.frames['myframe'];
        '    Session.RemoveAll()
        '    Session("usuario") = Usuario

        '    If Anthem.Manager.IsCallBack Then
        '        Anthem.Manager.AddScriptForClientSideEval("myframe.location.href='" + DirectCast(sender, System.Web.UI.WebControls.Menu).SelectedValue + "')")
        '    Else
        '        'Page.ClientScript.RegisterStartupScript(Me.GetType, "Menu_change", "<script>self.parent.frames[0].location.href='" + DirectCast(sender, System.Web.UI.WebControls.Menu).SelectedValue + "') </script>")
        '        'Response.Write("<script>self.parent.frames[0].src='" + DirectCast(sender, System.Web.UI.WebControls.Menu).SelectedValue + "') </script>")
        '        'Page.ClientScript.RegisterStartupScript(Me.GetType, "Menu_change", "<script>alert('" + DirectCast(sender, System.Web.UI.WebControls.Menu).SelectedValue + "') </script>")
        '        '
        '        ' Page.ClientScript.RegisterStartupScript(Me.GetType, "Menu_change", "<script>parent.myframe.location.href='" + DirectCast(sender, System.Web.UI.WebControls.Menu).SelectedValue + "' </script>")

        '        Page.ClientScript.RegisterStartupScript(Me.GetType, "Menu_change", "<script> parent.myframe.location.href='" + DirectCast(sender, System.Web.UI.WebControls.Menu).SelectedValue + "'; /*alert(parent.myframe.location.href)*/ </script>")


        '    End If

        'End If


    End Sub
End Class